package com.foreveross.fetl.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.foreveross.fetl.common.db.ProxoolDataSource;

public class DataBaseFetlUtils {
	
	
	private static ProxoolDataSource dataSource;
	
	public static ProxoolDataSource getDataSource() {
		return dataSource;
	}
	private static QueryRunner query  =null;
	public void setDataSource(ProxoolDataSource dataSource) {
		DataBaseFetlUtils.dataSource = dataSource;
		query = new QueryRunner(DataBaseFetlUtils.dataSource);
	}
	public static Connection getConnection() throws SQLException{
		return dataSource.getConnection();
	}
	
	public Connection getConn()throws SQLException{
		return dataSource.getConnection();
	}
	/**
	 * 返回查询到的数据
	 * @param sql
	 * @param o
	 * @param args
	 * @return
	 */
	public static <T> List<T> getBeanList(String sql, Class<T> o,Object... args) {
//		DbUtils.loadDriver(className);
		List<T> results = Collections.emptyList();
		try {
//			conn = DriverManager.getConnection(url, name, pass);
			// "select t.id,t.name from COAS_SOURCE t "
			results = (List<T>) query.query(sql, new BeanListHandler<T>(o),args);
			//System.out.println(results.size());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return results;
	}
	
	public static List<Map<String, Object>> getMapList(String sql, Object[] params) throws SQLException {  
        List<Map<String, Object>> list = null;  
		if (params == null) {  
		    list = (List<Map<String, Object>>) query.query(sql, new MapListHandler());  
		} else {  
		    list = (List<Map<String, Object>>) query.query(sql, new MapListHandler(), params);  
		}
        return list;  
    }

}
